<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
    <title>获取10个月的周数</title>
    <style>
        .highlight {
            color: red;
        }
        .week-item::before {
            content: "•";
            margin-right: 5px;
        }
    </style>
</head>
<body>
    <label for="start-date">请输入开始时间：</label>
    <input type="date" id="start-date" value="2024-03-25">
    <button onclick="calculateWeeks()">计算</button>
    <div id="weeks"></div>

    <script>
        function formatDate(date) {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            return `${year}年${month}月${day}日`;
        }

        function getWeeks(startDateString) {
            const startDate = new Date(startDateString);
            const endDate = new Date(startDate);
            endDate.setMonth(endDate.getMonth() + 10);

            const weeks = [];
            let currentDate = new Date(startDate);

            while (currentDate <= endDate) {
                const endDateOfWeek = new Date(currentDate);
                endDateOfWeek.setDate(endDateOfWeek.getDate() + 6);

                const week = {
                    start: currentDate,
                    end: endDateOfWeek
                };

                weeks.push(week);

                currentDate = new Date(endDateOfWeek);
                currentDate.setDate(currentDate.getDate() + 1);
            }

            return weeks;
        }

        function getCurrentWeekIndex(weeks) {
            const today = new Date();
            for (let i = 0; i < weeks.length; i++) {
                if (today >= weeks[i].start && today <= weeks[i].end) {
                    return i;
                }
            }
            return -1;
        }

        function displayWeeks(weeks, currentWeekIndex) {
            const weeksContainer = document.getElementById('weeks');
            weeksContainer.innerHTML = '';
            weeks.forEach((week, index) => {
                let weekDisplay = '';
                if (index === currentWeekIndex) {
                    weekDisplay += '* ';
                }
                weekDisplay += `第 ${index + 1} 周: ${formatDate(week.start)} to ${formatDate(week.end)}`;
                const weekElement = document.createElement('p');
                weekElement.className = 'week-item';
                weekElement.innerHTML = weekDisplay;
                if (index === currentWeekIndex) {
                    weekElement.classList.add('highlight');
                }
                weeksContainer.appendChild(weekElement);
            });
        }

        function calculateWeeks() {
            const startDateString = document.getElementById('start-date').value;
            const weeks = getWeeks(startDateString);
            const currentWeekIndex = getCurrentWeekIndex(weeks);
            displayWeeks(weeks, currentWeekIndex);
        }

        // 初始化显示
        calculateWeeks();
    </script>
</body>
</html>
